package com.crrcdt.mail.controller;


import cn.hutool.core.date.DateUtil;
import com.crrcdt.mail.config.RabbitConfig;
import com.crrcdt.mail.constant.MailConstants;
import com.crrcdt.mail.entity.Employee;
import com.crrcdt.mail.entity.MailSendLog;
import com.crrcdt.mail.service.EmployeeService;
import com.crrcdt.mail.service.MailSendLogService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liujun
 * @since 2020-09-29
 */
@RestController
@RequestMapping("/mail-send-log")
public class MailSendLogController {

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    EmployeeService employeeService;
    @Autowired
    MailSendLogService mailSendLogService;
    @RequestMapping("/add")
    public Map<String,String > insertMailSendLog(){
        final HashMap<String, String> map = new HashMap<>();
        final Employee employee = employeeService.getById(1);
        final MailSendLog mailSendLog = new MailSendLog();
        String msgId = UUID.randomUUID().toString();
        mailSendLog.setMsgId(msgId);
        mailSendLog.setCreateTime(new Date());
        mailSendLog.setExchange(MailConstants.MAIL_EXCHANGE_NAME);
        mailSendLog.setRouteKey(MailConstants.MAIL_ROUTING_KEY_NAME);
        mailSendLog.setEmpId(1);
        mailSendLog.setTryTime(new Date());
        mailSendLog.setUpdateTime(new Date());
        mailSendLog.setStatus(0);
        mailSendLog.setCount(0);
        final boolean save = mailSendLogService.save(mailSendLog);
        if(save){
            rabbitTemplate.convertAndSend(MailConstants.MAIL_EXCHANGE_NAME, MailConstants.MAIL_ROUTING_KEY_NAME, employee, new CorrelationData(mailSendLog.getMsgId()));
             map.put("msg","添加员工成功！");
             return map;
        }
        map.put("msg","添加员工失败！");
        return map;
    }
}

